跟別人不同的解法紀錄一下...
prob. desc.
- 機器人可以無窮的做
instructions
- return 在不限
instructions
次數的情況下,機器人是否在某輪instructions
執行的結尾回到原點
my sol.
- 紀錄座標(x, y)及面相(a, b),其中
(a, b)
表示複數a+bi
- 計算一輪後的(x, y, a, b)
- 回原點為 True
- 沒回就考慮面向
- 面向(a, b)沒變表示不可能回原點,return False
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
# x y a bi
mat = [0,0,0, 1]
for i in instructions:
if i =='G':
mat[0] += mat[2]
mat[1] += mat[3]
elif i == 'L':
mat[2], mat[3] = -mat[3], mat[2]
# (a+bi) *i
# = -b +ai
elif i == 'R':
mat[2], mat[3] = mat[3], -mat[2]
# (a+bi) *i^3
# = b -ai
return mat[:2] == [0, 0] or mat[2:] != [0,1]